﻿/**

 @Name：layuiAdmin 用户管理，增删改查
 @Author：马达
 @Site：http://www.sunplus.wang
 @License: 杭州九宸智能科技有限公司
    
 */

layui.define(['table', 'form', 'laydate', 'upload', 'admin'], function (exports) {
    var $ = layui.$
    , admin = layui.admin
    , view = layui.view
    , table = layui.table
    , upload = layui.upload
    , laydate = layui.laydate
    , form = layui.form;

    var nowdate = new Date();
    var varButtonRight = "";
    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , defaultToolbar: ['filter']
        , method: "post"
        , url: layui.setter.APIUrl + '/shop/goodsMgr/listPain'
        , headers: { //通过 request 头传递
            Authorization: "bearer " + layui.data('layuiAdmin').access_token
        }
        , parseData: function (res) { //res 即为原始返回的数据 
            varButtonRight = res.Data.ButtonRight;
            return {
                "ResultCode": res.ResultCode, //解析接口状态
                "ResultInfo": res.ResultInfo, //解析提示文本
                "count": res.Data.TotalRecordCount, //解析数据长度
                "Data": res.Data.Items //解析数据列表 
            };
        }
    , cols: [[
      { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100        
		//{
		//    field: 'Code', title: '商品编码/货号', width: 130, templet: function (d) {
		//        var html = "编码:<span class='font1'>" + d.Code + "</span>";
		//        html += "<br />货号:<span class='font1'>" + d.ArtNo + "</span>";
		//        return html;
		//    }
		//},
		{
		    field: 'Code', title: '商品编码', width: 110, templet: function (d) {
		        var html = "<span class='fontview' lay-event=\"view\" title='点击查看商品详情'>" + d.Code + "</span>"; 
		        if (d.GroupBuyInfo != null) {
		            html += "<br /><span class='bksp' id='tip" + d.Code + "' onclick=\"GroupBuyInfo('" + d.GroupBuyInfo.GroupPrice + "','" + d.GroupBuyInfo.StartTime + "','" + d.GroupBuyInfo.EndTime + "','" + d.GroupBuyInfo.IsAble_Sel + "', '" + d.Code + "')\">团购</span>";
		        }
		        return html;
		    }
		},
		{ field: 'ArtNo', title: '货号', minWidth: 100 },
		{ field: 'GoodsCategoryID_SelSourceName', title: '分类 <span class="orders" onclick="orders(3)" title="分类名称排序">⬇</span>', minWidth: 80 },
		{
		    field: 'Name', title: '名称<br/>副标题', minWidth: 320, templet: function (d) {
		        if (d.Title == null) d.Title = "";
		        var html = "<span class='font2'>" + d.Name + "</span>";
		        html += "<br /><span class='font3'>" + d.Title + "</span>";
		        return html;
		    }
		},
		{
		    field: 'Path_Upload', title: '封面', templet: function (d) {
		        var html = "<img src=" + layui.setter.ImgPath + d.Path_Upload + " alt='' />";
		        return html;
		    }, width: 90
		},
		{
		    field: 'SpecMode', title: '规格<br/>重量(kg)', minWidth: 100, templet: function (d) {
		        if (d.Title == null) d.Title = "";
		        var html = "规格：<span class='font1'>" + d.SpecMode + "</span>";
		        html += "<br />重量：<span class='font1'>" + d.Weight + "</span>";
		        return html;
		    }
		},
		//{
		//    field: 'DeliveMode', title: '配送方式<br/>是否包邮', minWidth: 100, templet: function (d) {
		//        var html = "<div>";
		//        var str = "";
		//        var ship = "";
		//        if (d.DeliveMode.indexOf("0") >= 0) {
		//            str = "自提";
		//            if (d.IsStorePick_Sel == 1) {
		//                str = "自提<span style='color:red' title='商品支持网点自提'>(网点)</span>";
		//            }
		//        }
		//        if (d.DeliveMode.indexOf("1") >= 0) {
		//            if (str != "") str += "、";
		//            str += "快递";
		//            if (d.IsShipping_Sel == 1) {
		//                ship += "<span style='color:#5FB878;font-weight:bold'>包邮</span>";
		//            }
		//        }
		//        html += "<span style='color:#01AAED;font-weight:bold'>" + str + "</span>";
		//        if (ship != "") {
		//            html += "<br />" + ship;
		//        }
		//        html += "</div>";
		//        return html;
		//    }
		//},
		{
		    field: 'Price', title: '价格(元)<br /><span class="orders" onclick="orders(1)" title="价格降序">⬇</span> <span class="orders" onclick="orders(2)" title="价格升序">⬆</span>', width: 90,
		    templet: function (d) {
		        var html = "<span style='color:#FF5722;font-weight:bold;'>￥</span><span style='color:#FF5722;font-weight:bold;font-size:1.1em'>" + d.Price + "</span>";
		        if (d.DisLables != "" && d.DisLables != null) {
		            var str = encodeURI(d.DisLables);
		            html += "<br /><span class='jgsp' id='jg" + d.Code + "' onclick=\"LookMemberPrice('" + str + "', '" + d.Code + "')\">....</span>";
		        }
		        return html;
		    }
		},
		{
		    field: 'Stock', title: '库存<br /><span class="orders" onclick="orders(4)" title="库存降序">⬇</span> <span class="orders" onclick="orders(5)" title="库存升序">⬆</span>', width: 70,
		    templet: function (d) {
		        var html = "<span style='font-weight:bold;font-size:1.1em'>" + d.Stock + "</span>";
		        return html;
		    }
		},
		//{
		//    field: 'LimitNum', title: '限购数量', width: 80, templet: function (d) {
		//        if (d.LimitNum <= 0) d.LimitNum = "无";
		//        var html = "<span style='font-weight:bold;font-size:1.1em'>" + d.LimitNum + "</span>";
		//        if (d.LimitArea != null && d.LimitArea != "") {
		//            var str = encodeURI(d.LimitArea);
		//            html += "<br /><span class='jgsp' id='area" + d.Code + "' onclick=\"LookLimitArea('" + str + "', '" + d.Code + "')\">....</span>";
		//        }
		//        return html;
		//    }
		//},
		{
		    field: 'IsShow_Sel', title: '状态', width: 70, templet: function (d) {
		        var Show = d.IsShow_Sel;
		        var str = "";
		        if (Show == 0) {
		            str = "<span style='color:#5FB878;font-weight:bold'>上架</span>";
		            if (d.SaleEndTime != null) {
		                var begdate = new Date(d.SaleBegTime);
		                var enddate = new Date(d.SaleEndTime);
		                var strbeg = encodeURI(d.SaleBegTime);
		                var strend = encodeURI(d.SaleEndTime);
		                if (begdate > nowdate) {
		                    str += "<i class=\"layui-icon icon1\" id='saletime" + d.Code + "' title='商品未到销售时间' onclick=\"LookSaleTime('" + strbeg + "', '" + strend + "', '" + d.Code + "')\">&#xe60b;</i>";
		                } else if (enddate < nowdate) {
		                    str += "<i class=\"layui-icon icon1\" id='saletime" + d.Code + "' title='商品已超过销售时间' onclick=\"LookSaleTime('" + strbeg + "', '" + strend + "', '" + d.Code + "')\">&#xe60b;</i>";
		                }
		            }
		        } else {
		            str = "<span style='color:#c2c2c2;font-weight:bold'>下架</span>";
		        }
		        var html = "" + str;
		        return html;
		    }
		},
		{
		    field: 'SupplierID_SelSourceName', title: '供应商<br />店铺', minWidth: 180, templet: function (d) {
		        var html = "";
		        html += "<span style='color:black;'>" + d.SupplierLevel + "</span>";
		        html += "<span style='color:black;'> " + d.SupplierArea + "</span>";
		        html += "<span> " + d.SupplierID_SelSourceName + "</span>";
		        html += "<br />";
		        html += "<span style='color:black;'>" + d.StoreArea + "</span>";
		        html += "<span> " + d.StoreID_SelSourceName + "</span>";
		        return html;
		    }
		},
      //{
      //    title: '操作', width: 80, fixed: 'right', align: 'center', templet: function (d) {
      //        var strRtn = "";
      //        if (varButtonRight.indexOf('edit') >= 0) {
      //            strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
      //        }
      //        return strRtn;
      //    }
      //}
    ]]
    , page: true
    , limit: 10
    , limits: [10, 15, 20, 25, 30]
    , text: {
        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
    }
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        //debugger;
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/goodsMgr/BatchDelete' //实际使用请改成服务端真实接口
                  , data: { "ids": data.PID }
                  , type: "post"
                    //, success: function (res) {
                    //}
                  , done: function (res) {
                      layer.close(index); //执行关闭 
                      table.reload('LAY-app-content-list');
                      layer.msg('删除成功');
                  }
                });
            });
        }
        else if (obj.event === 'edit') {
            //location.hash = '/shop/shop_goods/addChild/id=' + data.PID;
            admin.popup({
                title: '编辑信息'
                , area: ['80%', '96%']
                , zIndex: 1002
              , id: 'LAY-popup-content-edit'
              , success: function (layero, index) {
                  view(this.id).render('/shop/shop_goods/add', data).done(function () {
                      form.render(null, 'layuiadmin-app-form-list');

                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function (data) {
                          var field = data.field; //获取提交的字段 
                          if (field.Images == "" || field.Images == null || field.Images == "[]") {
                              layer.msg("请上传商品介绍图！");
                              return false;
                          }
                          if (field.IsShipping_Sel == 0 && field.DeliveMode.indexOf("1") >= 0 && (field.FreightID == "" || field.FreightID == null)) {
                              layer.msg("请选择运费模板！");
                              return false;
                          }
                          field["MemberPrice"] = GetMemberPrice();
                          //提交 Ajax 成功后，关闭当前弹层并重载表格 
                          admin.req({
                              url: layui.setter.APIUrl + '/shop/goodsMgr/update' //实际使用请改成服务端真实接口
                          , data: field
                          , type: "post"
                          , done: function (res) {
                              layui.table.reload('LAY-app-content-list'); //重载表格
                              layer.close(index); //执行关闭
                              layer.msg('修改成功');
                          }
                          });
                      });
                  });
              }
            });
        }
        else if (obj.event === 'view') {
            admin.popup({
                title: '查看信息'
                , area: ['80%', '96%']
             , id: 'LAY-popup-content-view'
             , success: function (layero, index) {
                 view(this.id).render('/shop/shop_goods/view', data).done(function () {
                     form.render(null, 'layuiadmin-app-form-list');
                     //监听提交
                     form.on('submit(layuiadmin-app-form-submit)', function (data) {
                         layer.close(index); //执行关闭
                     });
                 });
             }
            });
        }
    });

    exports('shop_goods', {})

});

function GroupBuyInfo(price, stime, etime, isable, code) {
    if (isable == 0) 
        isable = "显示"
    else
        isable = "不显示";;
    var content = "团购价：￥" + price;
    content += "<br />开始时间：" + stime;
    content += "<br />结束时间：" + etime;
    content += "<br />是否显示：" + isable;
    layer.tips(content, '#tip' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}

function LookMemberPrice(priceEnCode, code) {
    var str = decodeURI(priceEnCode);
    var Prices = JSON.parse(str);
    var content = "";
    layui.$.each(Prices, function (i, d) {
        if (content != "") {
            content += "<br />";
        }
        content += d.LevelStr + "：￥" + d.Price;
    })
    layer.tips(content, '#jg' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
function LookLimitArea(strEnCode, code) {
    var str = decodeURI(strEnCode);
    var content = "限购区域：<br />" + str;
    layer.tips(content, '#area' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
function LookSaleTime(strEnCode1, strEnCode2, code) {
    var str1 = decodeURI(strEnCode1);
    var str2 = decodeURI(strEnCode2);
    var content = "销售时间：<br />" + str1 + "<br />" + str2;
    layer.tips(content, '#saletime' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}

